require ('./common.js');
require ('./mysql_pool.js');

var mosca  = require('mosca'); 
global.MqttServer = new mosca.Server({
    port: 1885
});

//监听链接
global.MqttServer.on("clientConnected", function(client) {
    console.log("客户端连接:", client.id);
});

global.MqttServer.on("clientDisconnected", function(client) {
    console.log("客户端断开连接:", client.id);
});

global.MqttServer.on("published", function(packet, client) {//当客户端有连接的时候，发布主题消息
    var topic = packet.topic;
    console.log("published:", topic.substr(16));
    if (topic.substr(0, 15) === '/zq100l/dev/crd') {
    	var msg = packet.payload.toString();
    	
    	console.log("content:",msg);	

        global.wsboardcast(msg);
    }
});

global.MqttServer.on('ready', function(){
    console.log('mqtt服务器启动成功，正在等待连接...');  
});
